<?php
/* -----------------------------------------------------------------------------------------
   $Id: product_filter_listing.php 1286 2005-10-07 10:10:18Z mz $

   XT-Commerce - community made shopping
   http://www.xt-commerce.com

   Copyright (c) 2003 XT-Commerce
   -----------------------------------------------------------------------------------------
   based on:
   (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
   (c) 2002-2003 osCommerce(product_listing.php,v 1.42 2003/05/27); www.oscommerce.com 
   (c) 2003	 nextcommerce (product_listing.php,v 1.19 2003/08/1); www.nextcommerce.org

   Released under the GNU General Public License
   ---------------------------------------------------------------------------------------*/

$module_smarty = new Smarty;
$module_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');

$result = true;
// include needed functions
require_once (DIR_FS_INC.'xtc_get_all_get_params.inc.php');
require_once (DIR_FS_INC.'xtc_get_vpe_name.inc.php');
require_once (DIR_FS_INC.'xtc_date_short.inc.php');

if(isset($_GET['per_site']) && !empty($_GET['per_site']))
	$per_site = $_GET['per_site'];
elseif(isset($_SESSION['per_site']))
	$per_site = $_SESSION['per_site'];
elseif(!isset($_SESSION['per_site']) || !isset($_GET['per_site']))
	$per_site = MAX_DISPLAY_SEARCH_RESULTS;

$_SESSION['per_site'] = $per_site;

$listing_split = new splitPageResults($listing_sql, (int)$_GET['page'], (int)$per_site, 'p.products_id');

	if($_GET['view_as_filter'] !='') {
		$list_name = $_GET['view_as_filter'];
		$_SESSION['view_as_filter'] = $_GET['view_as_filter'];
	}
	elseif($_SESSION['view_as_filter'] !='')
		$list_name = $_SESSION['view_as_filter'];

	elseif(!isset($_SESSION['view_as_filter']) || !isset($_GET['view_as_filter'])) {
		$list_name = 'product_filter_list';
		$_SESSION['view_as_filter'] = 'product_filter_list';
	}


$module_content = array();
if ($listing_split->number_of_rows > 0) {
	$rows = 0;
	$cat_count = xtDBquery("SELECT id, titel, categories_ids FROM ".TABLE_PRODUCT_FILTER_CATEGORIES." WHERE language_id = '".$_SESSION['languages_id']."' AND status = '1'");
	$count = xtc_db_num_rows($cat_count);
	$listing_query = xtDBquery($listing_split->sql_query);

	while ($array = xtc_db_fetch_array($listing_query, true)) {
		$image='thumbnail';
		$tax_rate = $xtPrice->TAX[$array['products_tax_class_id']];
		$products_price = $xtPrice->xtcGetPrice($array['products_id'], $format = true, 1, $array['products_tax_class_id'], $array['products_price'], 1);

		if ($_SESSION['customers_status']['customers_status_show_price'] != '0') {
			if ($_SESSION['customers_status']['customers_fsk18'] == '1') {
				if ($array['products_fsk18'] == '0')
					$buy_now = $product->getBuyNowButton($array['products_id'], $array['products_name']);
					$wishlist = $product->getWishlistButton($array['products_id'], $array['products_name']);
			
			} else {
				$buy_now = $product->getBuyNowButton($array['products_id'], $array['products_name']);
				$wishlist = $product->getWishlistButton($array['products_id'], $array['products_name']);
			}
		}

			
		$shipping_status_name = $main->getShippingStatusName($array['products_shippingtime']);
		$shipping_status_image = $main->getShippingStatusImage($array['products_shippingtime']);
		$filter_ids = Array();
		$filters = xtDBquery(" SELECT filter_id FROM products_to_filter WHERE products_id = '".$array['products_id']."' ");
		
		while ($filter = xtc_db_fetch_array($filters)) {
		  $filter_ids[] = $filter['filter_id'];
		}
		
		$options = xtc_db_fetch_array(xtDBquery("SELECT 
														list_name, 
														col, 
														p_img, 
														p_name, 
														p_price, 
														b_details, 
														b_order, 
														b_wishlist, 
														p_reviews, 
														p_stockimg, 
														p_vpe, 
														p_model, 
														p_manu_img, 
														p_manu_name, 
														p_short_desc, 
														p_short_desc_lenght, 
														p_long_desc, 
														p_long_desc_lenght, 
														list_type 
													FROM 
														products_listings 
													WHERE 
														list_name = '".$list_name."' "));

		switch($options['col']) {
			case 2:
				$col_width = 'w474p';
				break;
			case 3:
				$col_width = 'w303p';
				break;
			case 4:
				$col_width = 'w223p';
				break;
			default:
				$col_width = 'w98p';
				break;
		}

		if((int)$count % (int)$options['col'] == 0) $col_class = 'last_li ';
		else $col_class = '';

		if((int)$options['col'] == 1) {
			$col_special_class = 'only_one';
			$module_smarty->assign('ONLY_ONE', 'only_one');
		} else {
			$col_special_class = 'gallery_item';
		}


		if(PRODUCT_LISTING_MANU_IMG == 'true' && ($options['p_manu_img'] == 1)) {
			// Hersteller ermitteln
			$manufacturer_query = xtDBquery("SELECT
													m.manufacturers_id,
													m.manufacturers_name,
													m.manufacturers_image,
													mi.manufacturers_url
												FROM
													" . TABLE_MANUFACTURERS . " m,
													" . TABLE_MANUFACTURERS_INFO . " mi
												WHERE 
													m.manufacturers_id = '".$array['manufacturers_id']."'
												AND 
													m.manufacturers_id = mi.manufacturers_id
												AND	
													mi.languages_id = '".(int)$_SESSION['languages_id']."'");
			$manufacturer = xtc_db_fetch_array($manufacturer_query,true);

			if(!empty($manufacturer['manufacturers_image']))
				$manufacturer_image = DIR_WS_IMAGES . $manufacturer['manufacturers_image'];
		}
		if(PRODUCT_LISTING_MANU_NAME == 'true'&& ($options['p_manu_name'] == 1)) {
			if(!empty($manufacturer['manufacturers_url']))
				$manufacturer_url = xtc_href_link(FILENAME_REDIRECT,'action=manufacturer&'.xtc_manufacturer_link($manufacturer['manufacturers_id'],$manufacturer['manufacturers_name']));
			if(!empty($manufacturer['manufacturers_name']))
				$manufacturer_name = $manufacturer['manufacturers_name'];
		}



		if(PRODUCT_LISTING_VPE == 'true' && ($options['p_vpe'] == 1)) {
			$products_price = $xtPrice->xtcGetPrice($array['products_id'], false, 0, $array['products_tax_class_id'], $array['products_price']);
			$vpe = $product->getVPEtext($array, $products_price);
		}
		if(PRODUCT_LISTING_MODEL == 'true' && ($options['p_model'] == 1))
			$model = $array['products_model'];
		if($options['p_stockimg'] == 1) {
			require_once (DIR_FS_INC.'cseo_get_stock_img.inc.php');
			$stock_img = cseo_get_stock_img($array['products_quantity']);
		}
		if($options['b_details'] == 1)
			$button_details = '<a title="'.$array['products_name'].'" href="'.xtc_href_link(FILENAME_PRODUCT_INFO, xtc_product_link($array['products_id'], $array['products_name'])).'">'.xtc_image_button('button_details.gif', 'Details').'</a>';

		require_once (DIR_FS_INC.'cseo_truncate.inc.php');
		if($options['p_short_desc'] == 1)
			$description = cseo_truncate($array['products_short_description'], $options['p_short_desc_lenght']);
		elseif($options['p_long_desc'] == 1)
			$description = cseo_truncate($array['products_description'], $options['p_long_desc_lenght']);
		else
			$description = '';
			
		#Bilder Attribute mit Mehrwert
		$description_img_alt = cseo_truncate(strip_tags($array['products_short_description']), 35);
		
		
		$module_content[] =	Array(
				'COUNT' => $count,
				'COL_WIDTH' => $col_width,
				'COL_CLASS' => $col_class,
				'COL_SPECIAL' => $col_special_class,
				'PRODUCTS_NAME' => (($options['p_name'] == 1)?htmlentities($array['products_name'], ENT_COMPAT,charset_mapper($_SESSION['language_charset'])):''), 
				'COUNT'=>$array['ID'],
				'PRODUCTS_ID'=>$array['products_id'],
				'PRODUCTS_VPE' => $product->getVPEtext($array, $products_price['plain']), 
				'PRODUCTS_IMAGE' => $product->productImage($array['products_image'], $image), 
				'PRODUCTS_IMAGE_DIMENSION' => cseo_get_img_size($product->productImage($array['products_image'], $image)),
				'PRODUCTS_IMAGE_ALT' => $description_img_alt,
				'PRODUCTS_LINK' => xtc_href_link(FILENAME_PRODUCT_INFO, xtc_product_link($array['products_id'], $array['products_name'])), 
				'PRODUCTS_PRICE' => $products_price['formated'], 
				'PRODUCTS_MANUFACTURER_IMG' => $manufacturer_image,
				'PRODUCTS_MANUFACTURER_URL' => $manufacturer_url,
				'PRODUCTS_MANUFACTURER_NAME' => $manufacturer_name,
				'PRODUCTS_MODEL' => $model,
				'PRODUCTS_WEIGHT' => $array['products_weight'],
				'PRODUCTS_TAX_INFO' => $main->getTaxInfo($tax_rate), 
				'PRODUCTS_SHIPPING_LINK' => $main->getShippingLink(), 
				'PRODUCTS_BUTTON_BUY_NOW' => (($options['b_order']==1)?$buy_now:''),
				'PRODUCTS_REVIEWS' => $reviews_img,
				'PRODUCTS_STOCK_IMG' => $stock_img,
				'PRODUCTS_SHIPPING_NAME'=>$shipping_status_name,
				'PRODUCTS_SHIPPING_IMAGE'=>$shipping_status_image, 
				'PRODUCTS_EXPIRES' => $array['expires_date'],
				'PRODUCTS_DATE' => xtc_date_short($array['products_date_available']),
				'PRODUCTS_CATEGORY_URL'=>$array['cat_url'],
				'PRODUCTS_BUTTON_WISHLIST' => (($options['b_wishlist']==1)?$wishlist:''),
				'PRODUCTS_BUTTON_DETAILS' => $button_details,				
				'FILTER_IDS' => $filter_ids,
				'PRODUCTS_DESCRIPTION' => $description,
				'PRODUCTS_FSK18' => $array['products_fsk18']);
	}
} else {
	// no product found
	$result = false;
}


if ($result != false) {

	$navigation_smarty = new Smarty;

	$file_name = FILENAME_PRODUCT_FILTER.'?request';
	$page_links = $listing_split->getLinksArrayFilter(MAX_DISPLAY_PAGE_LINKS, $get_param, TEXT_DISPLAY_NUMBER_OF_PRODUCTS);
	
	$navigation_smarty->assign('LINKS', $page_links);
	$navigation_smarty->assign('language', $_SESSION['language']);
	$navigation_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
	$navigation = $navigation_smarty -> fetch(CURRENT_TEMPLATE.'/module/products_page_navigation.html');
	
	$getCols = xtc_db_fetch_array(xtDBquery("SELECT col FROM products_listings WHERE list_name ='".$list_name."'"));

	if(isset($_GET['page']) && $_GET['page'] !='')
		$page .= '&page='.$_GET['page'];


	switch($getCols['col']) {
		case '3' :
			$view_per_site  = ($per_site == 9 ? '<b>9</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=9'.$get_param).'">9</a>').' | ';
			$view_per_site .= ($per_site == 18 ? '<b>18</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=18'.$get_param).'">18</a>').' | ';
			$view_per_site .= ($per_site == 27 ? '<b>27</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=27'.$get_param).'">27</a>').' | ';
			$view_per_site .= ($per_site == 45 ? '<b>45</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=45'.$get_param).'">45</a>').' | ';
			$view_per_site .= ($per_site == 81 ? '<b>81</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=81'.$get_param).'">81</a>');
			break;

		case '4' :
			$view_per_site  = ($per_site == 12 ? '<b>12</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=12'.$get_param).'">12</a>').' | ';
			$view_per_site .= ($per_site == 24 ? '<b>24</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=24'.$get_param).'">24</a>').' | ';
			$view_per_site .= ($per_site == 60 ? '<b>60</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=60'.$get_param).'">60</a>').' | ';
			$view_per_site .= ($per_site == 84 ? '<b>84</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=84'.$get_param).'">84</a>').' | ';
			$view_per_site .= ($per_site == 96 ? '<b>96</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=96'.$get_param).'">96</a>');
			break;

		default :
			$view_per_site  = ($per_site == 10 ? '<b>10</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=10'.$get_param).$page.'">10</a>').' | ';
			$view_per_site .= ($per_site == 20 ? '<b>20</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=20'.$get_param).$page.'">20</a>').' | ';
			$view_per_site .= ($per_site == 30 ? '<b>30</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=30'.$get_param).$page.'">30</a>').' | ';
			$view_per_site .= ($per_site == 50 ? '<b>50</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=50'.$get_param).$page.'">50</a>').' | ';
			$view_per_site .= ($per_site == 100 ? '<b>100</b>' : '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('per_site', 'page', 'x', 'y', 'view_as')).'&per_site=100'.$get_param).$page.'">100</a>');
			break;
	}

	$per_site_html = new Smarty;
	$per_site_html->assign('LINKS_PER_SITE', $view_per_site);
	$per_site_html->assign('language', $_SESSION['language']);
	$products_persite = $per_site_html->fetch(CURRENT_TEMPLATE.'/module/products_per_site.html');

	switch($list_name) {
		case 'product_filter_list' :
			$views_as = '<a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('x', 'y', 'page', 'per_site', 'view_as_filter')).'&view_as_filter=product_filter_grid'.$get_param).$page.'">'. LISTING_GALLERY .'</a> '. LISTING_LIST_ACTIVE;
			break;
		default :
			$views_as = LISTING_GALLERY_ACTIVE .' <a href="'.xtc_href_link($file_name. xtc_get_all_get_params(array('x', 'y', 'page', 'per_site', 'view_as_filter')).'&view_as_filter=product_filter_list'.$get_param).$page.'">'. LISTING_LIST .'</a>';
			break;
	}

	$view = new Smarty;
	$view->assign('LINKS_VIEW_AS', $views_as);
	$view->assign('language', $_SESSION['language']);
	$views = $view->fetch(CURRENT_TEMPLATE.'/module/products_view_as.html');
	$module_smarty->assign('PRODUCTS_VIEW_AS', $views);	

	$module_smarty->assign('FILTER_SEARCH', $_SESSION['filters']);
	$module_smarty->assign('PRODUCTS_PER_SITE', $products_persite);
	$module_smarty->assign('NAVIGATION', $navigation);	
	if($name !='') $module_smarty->assign('filter_request',$namen);
	$module_smarty->assign('language', $_SESSION['language']);
	$module_smarty->assign('module_content', $module_content);
	$module_smarty->assign('product_filter_list', true);
	$module_smarty->assign('CLASS', 'product_filter_list');

	// set cache ID
	$module_smarty->caching = 0;
	$module = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/product_listings.html');
	
	$smarty->assign('main_content', $module);
	
	
} else {
	$error = TEXT_PRODUCT_NOT_FOUND;
	include (DIR_WS_MODULES.FILENAME_ERROR_HANDLER);
}
$smarty->display(CURRENT_TEMPLATE.'/index.html');
?>